package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.PrevDaySuiweiController;
import com.antoco.operation.bigscreen.model.dto.PrevDaySuiweiDTO;
import com.antoco.operation.bigscreen.model.entity.PrevDaySuiwei;
import com.antoco.operation.bigscreen.mapper.PrevDaySuiweiMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.PrevDaySuiweiService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/prev-day-suiwei")
@RestController
public class PrevDaySuiweiControllerImpl implements PrevDaySuiweiController {

    @Autowired
    private PrevDaySuiweiService prevDaySuiweiService;

    @Autowired
    private PrevDaySuiweiMapper prevDaySuiweiMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public PrevDaySuiweiDTO save(@Validated @RequestBody PrevDaySuiweiDTO prevDaySuiweiDTO) {
        PrevDaySuiwei prevDaySuiwei = prevDaySuiweiMapper.asEntity(prevDaySuiweiDTO);
        prevDaySuiweiService.save(prevDaySuiwei);
        channelDataService.pushAllLists(this.getClass().getName());
        return prevDaySuiweiDTO;
    }

    @Override
    @GetMapping("/{id}")
    public PrevDaySuiweiDTO findById(@PathVariable("id") String id) {
        PrevDaySuiwei prevDaySuiwei = prevDaySuiweiService.findById(id).orElse(null);
        return prevDaySuiweiMapper.asDTO(prevDaySuiwei);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        prevDaySuiweiService.deleteById(id);
    }

    @Override
    @GetMapping
    public List<PrevDaySuiweiDTO> list() {
        return prevDaySuiweiMapper.asDTOList(prevDaySuiweiService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<PrevDaySuiweiDTO> pageQuery(Pageable pageable) {
        Page<PrevDaySuiwei> prevDaySuiweiPage = prevDaySuiweiService.findAll(pageable);
        List<PrevDaySuiweiDTO> dtoList = prevDaySuiweiPage
                .stream()
                .map(prevDaySuiweiMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, prevDaySuiweiPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public PrevDaySuiweiDTO update(@RequestBody PrevDaySuiweiDTO prevDaySuiweiDTO, @PathVariable("id") String id) {
        PrevDaySuiwei prevDaySuiwei = prevDaySuiweiMapper.asEntity(prevDaySuiweiDTO);
        prevDaySuiweiService.update(prevDaySuiwei, id);
        channelDataService.pushAllLists(this.getClass().getName());
        return prevDaySuiweiDTO;
    }
}